<?php include APP_ADMIN_VIEW . 'include/head.php';?>
    <link rel="stylesheet" href="/static/write/css/editormd.css" />

<div class="panel panel-default">
                    <div class="panel-heading">新建文章</div>
                    <div class="panel-body">

<form id='post_form' action='/<?php echo APP_ADMIN_URI; ?>/post/create' method="POST">
<input type="hidden" name="hash" value='<?php echo isset($post_data['hash']) ? $post_data['hash'] : ''; ?>'/>
<div class="form-group">
<label>
标题：
<input type="text" class="form-control" style="width: 600px;"name="title" value="<?php echo isset($post_data['title']) ? $post_data['title'] : ''; ?>"/>
</label>
</div>

<div class="form-group">

<label>内容</label>

<div id="content-editor"></div>
        <textarea id="content" name="content" style="display: none;"><?php echo isset($post_data['content']) ? $post_data['content'] : ''; ?></textarea>

</div>

<div class="form-group">

    <label>

    关键词：
    <input type="text"  name="keywords" class="form-control"  value="<?php echo isset($post_data['keywords']) ? $post_data['keywords'] : ''; ?>"/>
    </label>
</div>

<div class="form-group">
    <label>
        简介：<textarea rows="2" cols="60" class="form-control"  name="description"><?php echo isset($post_data['description']) ? $post_data['description'] : ''; ?></textarea>
    </label>
</div>
<div class="form-group">

    <label>
        分类：
<input type="text" class="form-control" style="width: 200px;" id="new_category_name" value=""/>


 </label>
 <input type="button" class="btn btn-default" id="new_category_btn" onclick="newCategory()" value="创建分类"/>
</div>
<div class="form-group">

    <span>文章分类：</span>
    <select id="category_id" name="category_id" calss='form-control' style="width:200px">
        <option value="0" selected="selected">未分类</option>
    </select>
</div>
<div class="form-group">

    <span>发布状态：</span>
    <select name="status" calss='form-control'>
        <option value="1" <?php if (isset($post_data['status']) && $post_data['status'] == 1): ?>selected="selected"<?php endif?>>发布</option>
        <option value="0" <?php if (isset($post_data['status']) && $post_data['status'] == 0): ?>selected="selected"<?php endif?>>未发布</option>
    </select>
</div>
<div class="form-group">

    <span>草稿状态：</span>
    <select name="draft" calss='form-control'>
        <option value="0" <?php if (isset($post_data['draft']) && $post_data['draft'] == 0): ?>selected="selected"<?php endif?> >正常</option>
        <option value="1" <?php if (isset($post_data['draft']) && $post_data['draft'] == 1): ?>selected="selected"<?php endif?>>草稿</option>
    </select>

</div>
<div class="form-group">

         <button class="btn btn-primary" onclick="savePost()">保存</button>&nbsp;&nbsp;
         <a href="/<?php echo APP_ADMIN_URI; ?>/post/list" class="btn btn-default" style="" >返回列表</a>
</div>

</form>
</div>
</div>


<script src="/static/write/editormd.js"></script>
<script type="text/javascript">
    var contentMdEditor;

    $(function() {
        md=$('#content').val();
        contentMdEditor = editormd("content-editor", {
                width: "100%",
                height: 740,
                path : '/static/write/lib/',
                theme : "default",
                previewTheme : "default",
                editorTheme : "default",
                markdown : md,
                codeFold : true,
                //syncScrolling : false,
                saveHTMLToTextarea : true,    // 保存 HTML 到 Textarea
                searchReplace : true,
                //watch : false,                // 关闭实时预览
                htmlDecode : "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
                //toolbar  : false,             //关闭工具栏
                //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
                emoji : true,
                taskList : true,
                tocm            : true,         // Using [TOCM]
                tex : true,                   // 开启科学公式TeX语言支持，默认关闭
                flowChart : true,             // 开启流程图支持，默认关闭
                sequenceDiagram : true,       // 开启时序/序列图支持，默认关闭,
                //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
                //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
                //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
                //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
                //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
                imageUpload : true,
                imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL : "/upload",
                onload : function() {
                    console.log('onload', this);
                    //this.fullscreen();
                    //this.unwatch();
                    //this.watch().fullscreen();

                    //this.setMarkdown("#PHP");
                    //this.width("100%");
                    //this.height(480);
                    //this.resize("100%", 640);
                }
            });
    });
</script>

<script type="text/javascript">

    function savePost() {
        var md=contentMdEditor.getMarkdown();
       $('#content').text(md);
       document.getElementById('post_form').submit();
    }
    function newCategory(){
        category_name=$('#new_category_name').val();
        $.post("/<?php echo APP_ADMIN_URI; ?>/category/quick_create",{category_name:category_name},function(result){
            if(result=='1'){
               alert("分类名称不能为空！");
            }else if(result!='0' && result!=''){
                categorySelect=$("#category_id");
                categorySelect.append(result);//添加option
                categorySelect.find("option:last").attr("selected",true);
            }else {
                alert("分类创建失败。");
            }

          });

    }

 $(function() {// 初始化内容
    //加载分类数据
 $.ajax({
            url:"/<?php echo APP_ADMIN_URI; ?>/category/json",
            data:{},
            type:"POST",
            dataType:"json",
            timeout:5000,
            success:function(json){
                $.each(json, function (n, value) {
                    current_id=<?php echo isset($post_data['category_id']) ? intval($post_data['category_id']) : 0; ?>;
                    selected_str="";
                    if(current_id==value.id){
                        selected_str=" selected='selected' ";
                    }
                    options_str="<option value='"+value.id+"'"+selected_str+">"+value.category_name+"</option>";

                    $("#category_id").append(options_str);
                });

            },
            error:function(xhr, status){
                alert("请求出错。");
            }
    });
});
</script>
<?php include APP_ADMIN_VIEW . 'include/foot.php';?>